package com.apobates.jforum2.troll.utils.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 分页结果值对象
 * @param <T>
 */
public class EntityPageResult<T extends Object> implements Serializable {
    /**
     * 总数量
     */
    private long total;
    /**
     * 页码值
     */
    private int page;
    /**
     * 每页显示的数量
     */
    private int size;
    /**
     * 结果集
     */
    private List<T> result;

    public EntityPageResult() {
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int pageSize) {
        this.size = pageSize;
    }

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }
    /**
     * 
     * @param <T>
     * @param pageSize 页码
     * @return 
     */
    public static <T extends Object> EntityPageResult<T> empty(int pageSize){
        EntityPageResult<T> rs = new EntityPageResult<>();
        rs.setTotal(0L);
        rs.setPage(1);
        rs.setSize(pageSize);
        rs.setResult(Collections.emptyList());
        return rs;
    }
    /**
     * 
     * @param <T>
     * @param total 总数量
     * @param page 页码值
     * @param size 每页显示的数量
     * @param result 结果集
     * @return 
     */
    public static <T extends Object> EntityPageResult<T> of(long total, int page, int size, Stream<T> result){
        EntityPageResult<T> rs = new EntityPageResult<>();
        rs.setTotal(total);
        rs.setPage(page);
        rs.setSize(size);
        rs.setResult(result.collect(Collectors.toList()));
        return rs;
    }
    public static <T extends Object> EntityPageResult<T> of(long total, int page, int size, Collection<T> result){
        EntityPageResult<T> rs = new EntityPageResult<>();
        rs.setTotal(total);
        rs.setPage(page);
        rs.setSize(size);
        rs.setResult(new ArrayList<>(result));
        return rs;
    }
}